package com.briup.smartcity.web.controller;

import com.briup.smartcity.aop.Logging;
import com.briup.smartcity.bean.Article;
import com.briup.smartcity.bean.Category;
import com.briup.smartcity.exception.ServiceException;
import com.briup.smartcity.service.ICategoryService;
import com.briup.smartcity.utils.Result;
import com.briup.smartcity.utils.ResultUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "资讯栏目模块")
@RestController
@CrossOrigin
@RequestMapping("/auth/category")
public class CategoryController {
    @Autowired
    private ICategoryService service;
    @Logging()
    @DeleteMapping("/deleteById")
    @ApiOperation("通过id删除栏目")
    public Result deleteById(Integer id) {
        List<Category> categories = service.findEqual();
        List<Article> article=service.findAllArticle();
        for (Category category : categories) {
            if (category.getCategoryId() == id) {
                System.out.println(id);
                System.out.println(category.getCategoryId());
                throw new ServiceException("该栏目带有子栏目,请先删除子栏目");
            }
        }
        for (Article article1 : article) {
            if (article1.getArticleCategoryId()==id) {
                throw new ServiceException("该栏目下有资讯信息，请先删除资讯信息");
            }
        }

        service.deleteById(id);//web->service
        return ResultUtil.success();
    }

    @GetMapping("/find/all")
    @ApiOperation("查找所有栏目不分页")
//    @Logging()
    public Result selectAll() {
        List<Category> categories = service.findAll();
        return ResultUtil.success(categories);
    }

    @GetMapping("/page/queryAll")
    @ApiOperation("分页查找栏目信息")
//    @Logging()
    public Result selectPageHelp(@RequestParam("pageNum") int num, @RequestParam("pageSize") int size) {
        PageInfo<Category> all = service.pageQueryAll(num, size);
        return ResultUtil.success(all);
    }

    @ApiOperation("保存栏目信息")
    @PostMapping("/save")
    public Result saveCategory(Category category) {
        service.saveCategory(category);
        return ResultUtil.success();
    }

    @ApiOperation("更新栏目信息")
    @PutMapping("/update")
    public Result updateCategory(Category category) {
        service.updateCategory(category);
        return ResultUtil.success();
    }

    @ApiOperation("批量删除栏目")
    @DeleteMapping("/batchDelete")
    public Result deleteAllById(int[] ids) {
        List<Category> list = service.findEqual();
        for (int i = 0; i < ids.length; i++) {
            int d = ids[i];
            for (Category category : list) {
                if (category.getCategoryId() == d) {
                    throw new ServiceException("删除的栏目中有些栏目带有子栏目，请先删除子栏目");
                }
            }
        }
        service.batchDelete(ids);
        return ResultUtil.success();
    }

}
